package com.zsk.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.zsk.bean.ColParams;
import com.zsk.bean.TableParams;
import com.zsk.service.IEntityCodeGenService;
import com.zsk.util.FileUtil;
import com.zsk.util.MvnPrjUtil;

@Service
public class EntityCodeGenServiceImpl implements IEntityCodeGenService {

	@Override
	public boolean genEntityCode(List<TableParams> list, String packageName, String groupId, String artifactId) {
		for (TableParams tableInfo : list)
			genEntity(tableInfo, packageName, artifactId);
		return true;
	}

	protected void genEntity(TableParams tableInfo, String packageName, String artifactId) {
		String name = "entity";
		String nameExt = "Entity";
		String dir = MvnPrjUtil.getWorkCodeDir(artifactId, packageName) + name;
		String tableName = tableInfo.getTableName();

		StringBuilder sbCol = new StringBuilder();
		List<ColParams> cols = tableInfo.getTableCol();
		cols.forEach(e -> {
			String type = "String";
			switch (e.getColType()) {
			case "String":
				type = "String";
				break;
			case "Integer":
				type = "Integer";
				break;
			case "Boolean":
				type = "Boolean";
				break;
			case "Date":
				type = "Date";
				break;

			default:
				break;
			}
			sbCol.append("private " + type + " " + e.getColName() + ";\n    ");
		});

		String className = MvnPrjUtil.getClassName(tableName);
		String content = FileUtil.readTxtFile("template/EntityTemplate.java").toString();
		content = content.replace("#1", packageName + "." + name);
		content = content.replace("#2", className);
		content = content.replace("#3", className + nameExt);
		content = content.replace("#4", sbCol);

		FileUtil.writeTxtFile(content, dir + "/" + className + nameExt + ".java");
	}

}
